package cn.itcast.bos.service.take_delivery.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.take_delivery.PromotionRepository;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.take_delivery.PromotionService;


@Service
@Transactional
public class PromotionServiceimpl implements PromotionService{
    //注入dao
	@Autowired
	private PromotionRepository promotionRepository;
	
	//保存宣传任务
	@Override
	public void save(Promotion promotion) {
		promotionRepository.save(promotion);
	}

	//分页查询 
	@Override
	public Page<Promotion> findPageDate(Pageable pageable) {
		return promotionRepository.findAll(pageable);
	}

	//根据page 和rows 返回分页数据
	@Override
	public PageBean<Promotion> findPageDaate(int page, int rows) {
		System.out.println("分页"+page+rows);
		Pageable  pageable = new PageRequest(page -1, rows);
		//查询到page对象 当前页数据和总记录数都在该对象中
		Page<Promotion> pageData = promotionRepository.findAll(pageable);
		// 把前台需要的数据都封装到page对象 把这个pagBean返回给请求端
		PageBean<Promotion> pageBean = new PageBean<Promotion>();
		pageBean.setTotalCount(pageData.getTotalElements());
		pageBean.setPageDate(pageData.getContent());
		return pageBean;
	}
    
	
	//据id查询到指定的promotion对象返回给请求端
	@Override
	public Promotion findById(Integer id) {
		return promotionRepository.findOne(id);
	}
   
	
	//设置宣传活动过期
	@Override
	public void updateStatus(Date date) {
	        promotionRepository.updateStatus(date);
	}

}
